package com.iaiai.browser.activity.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Picture;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.iaiai.browser.activity.R;
import com.iaiai.browser.core.util.HttpUtil;
import com.iaiai.browser.core.util.Message;
import com.iaiai.browser.core.widget.Panel;

/**
 * 
 * <p>
 * Title: IndexController.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-2
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class WebManager {

	public interface Hander {
		void loading(); // 加载

		void unLoading(); // 取消加载
		
		void open(String url);	//打开
		void selected(int index);	//选择
		
		void showIndex();	//显示首页
	}

	private LabelManager labelManager; // 标签管理

	private BodyManager bodyManager; // 体管理

	private ImageView go;

	private Context context;

	private EditText urlEdit; // url文本框

	private Panel topPanel;

	private Hander handler;

	public void setHandler(Hander handler) {
		this.handler = handler;
	}

	public WebManager(Context context) {
		this.context = context;

		init();
		setOnClickListener();
	}

	private Activity getActivity() {
		return (Activity) context;
	}

	private void init() {
		labelManager = new LabelManager(context); // 创建标签管理类
		bodyManager = new BodyManager(context);
		go = (ImageView) getActivity().findViewById(R.id.go);
		urlEdit = (EditText) getActivity().findViewById(R.id.url);
		topPanel = (Panel) getActivity().findViewById(R.id.topPanel);
		
		topPanel.setOpen(true, true); // 打开顶部菜单
	}
	
	/**
	 * 打开
	 * @param uri 地址
	 */
	public void open(String uri){
		// 判断url是否正确
		if (!HttpUtil.isUrl(urlEdit.getText().toString())) {
			Message.showToast(getActivity(), "请输入有效的URL地址!");
			return;
		}

		String url = uri.replaceAll("\\\\", "/");
		if (!url.toLowerCase().startsWith("http://")) {
			url = "http://" + url;
		}

		closeInput();	//关闭输入法

		handler.loading(); // 加载loading...

		topPanel.setOpen(false, false); // 关闭顶部菜单
		labelManager.addLabel(url); // 添加标签
		bodyManager.addWebView(url); // 添加webView

		urlEdit.setText(""); // 清空

		handler.unLoading();
	}
	
	/**
	 * 关闭输入法
	 */
	private void closeInput(){
		// 获取输入法打开的状态
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(go.getWindowToken(), 0);
	}

	private void setOnClickListener() {
		go.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				open(urlEdit.getText().toString());	//打开
			}
		});

		labelManager.setHandler(new LabelManager.Handler() {
			public void selected(int index) {
				bodyManager.show(index);
				handler.selected(index);
			}
			public void loading() {
				handler.loading();
			}
			public void unLoading() {
				handler.unLoading();
			}
			public void remove(int index) {
				bodyManager.remove(index);
			}
			public void showIndex() {
				bodyManager.showIndex();
			}
		});
		bodyManager.setHandler(new BodyManager.Handler() {
			public void loading(int index) {
				labelManager.showLoading(index);
//				handler.loading();
			}
			public void unLoading(int index) {
//				handler.unLoading();
				labelManager.hiddenLoading(index);
			}
			public void onIcon(int index, Bitmap icon) {
				labelManager.setIcon(index, icon);
			}
			public void onTitle(int index, String title) {
				labelManager.setTitle(index, title);
			}
			public void open(String url) {
				handler.open(url);
			}
			public void showIndex() {
				handler.showIndex();	//显示首页
			}
		});
	}
	
	/**
	 * 获得所选择的url
	 * @return
	 */
	public String getSelUrl(){
		return bodyManager.getSelUrl();
	}
	
	/**
	 * 获得所选择的标题
	 * @return
	 */
	public String getSelTitle(){
		return labelManager.getSelTitle();
	}
	
	/**
	 * 获得当前截图
	 * @return
	 */
	public Bitmap getSelScreen(){
		return bodyManager.getSelScreen();
	}
	
	/**
	 * 刷新
	 */
	public void ref(){
		bodyManager.ref();
	}
	
	/**
	 * 向前
	 */
	public void goForward(){
		bodyManager.goForward();
	}
	
	/**
	 * 向后
	 */
	public void goBack(){
		bodyManager.goBack();
	}
	
	/**
	 * 显示home页
	 */
	public void showHome(){
		labelManager.show(-1);	//不选择任何
		bodyManager.showHome();	//显示home
	}

}
